<?php
namespace app\index\controller;
use app\common\controller\Homebase;
class Authgroup extends Homebase
{
    public function show()
    {
    	$data = db('auth_group')->where('status',1)->where('id','<>',2)->select();
    	$this->assign('data',$data);
        return $this->fetch();
    }

    public function edit(){
    	if($this->request->isPost()){
    		$id = $this->request->param('id');
    		$title = $this->request->param('title');
    		$res = db('auth_group')->where('id',$id)->update(['title'=>$title]);
    		if($res!==false){
    			return ['code'=>1,'msg'=>'修改成功'];
    		}else{
    			return ['code'=>0,'msg'=>'修改失败'];
    		}
    	}
    	$id = $this->request->param('id');
    	$this->assign('data',db('auth_group')->where('id',$id)->find());
    	return $this->fetch();
    }
    public function del(){
    	if($this->request->isPost()){
    		$id = $this->request->param('id');
    		$res = db('auth_group')->where('id',$id)->update(['status'=>0]);
    		if($res!==false){
    			return ['code'=>1,'msg'=>'删除成功'];
    		}else{
    			return ['code'=>0,'msg'=>'删除失败'];
    		}
    	}
    }

    public function add(){
    	if($this->request->isPost()){
    		$title = $this->request->param('title');
    		if(!$title){
    			return ['code'=>0,'msg'=>'请输入岗位名'];
    		}
    		$res = db('auth_group')->insert(['title'=>$title]);
    		if($res){
    			return ['code'=>1,'msg'=>'添加成功'];
    		}else{
    			return ['code'=>0,'msg'=>'添加失败'];
    		}    		
    	}
    }

    public function rulelist(){
    	if($this->request->isPost()){
    		$id = $this->request->param('id');
    		$ids = $this->request->param('ids');
    		$res = db('auth_group')->where('id',$id)->update(['rules'=>$ids]);
    		if($res!==false){
    			return ['code'=>1,'msg'=>'更新成功'];
    		}else{
    			return ['code'=>0,'msg'=>'更新失败'];
    		}
    	}
    	$id = $this->request->param('id');
    	$group = db('auth_group')->where('id',$id)->find();
    	$this->assign('group',$group);
    	$this->assign('rules',explode(',',$group['rules']));
    	return $this->fetch();
    }
}
